﻿
component.Component = function(context,id,containerid,buttonName){	
	this.context = context;
	this.id=id;
	this.container = (containerid==null || containerid=="undefined")?document.body:document.getElementById(containerid);
	this.buttonName = (buttonName=="null" || buttonName==null || buttonName=="undefined")?"选择":buttonName;

	this.buttonObj = null;
	this.idObj = null;
	this.viewObj = null;
	
	this.windowWidth = 500;
	this.windowHeight = 600;
	
	this.condition = "";
	this.paras = "";//url参数
	if(!(id==null || id=="undefined")){
		this.paras = "&id="+id;
	}
}

component.Component.prototype = {
	isNull:function(value){
		if(value==null || value=="" || value=="undefined"){
			return true;
		}
		return false;
	},
	//创建组件选择框所需要的文本框和按钮
	createSelectElement:function(idName,idValue,nameName,nameValue,size,className){
		if(!this.isNull(idName)){
			this.idObj = document.getElementById(idName);
			if(this.idObj==null){
				var idViewObj = document.createElement("input");
				idViewObj.type = "hidden";
				idViewObj.name = idName;
				idViewObj.value = idValue;
				this.idObj = idViewObj;
				this.container.appendChild(this.idObj);
			}
		}
		if(!this.isNull(nameName)){
			this.viewObj = document.getElementById(nameName);
			if(this.viewObj==null){
				var nameViewObj = document.createElement("input");
				nameViewObj.type = "text";
				nameViewObj.name = nameName;
				nameViewObj.value = nameValue;
				nameViewObj.size = size;
				nameViewObj.className = className;
				this.viewObj = nameViewObj;
				this.container.appendChild(this.viewObj);
			}
		}
		
		this.createButton();
	},
	createNormalButton:function(container,name,onclickMethod){
		var buttonObj = this.createNormalButton(name);
		buttonObj.onclick = function(){
			eval(onclickMethod);
		}
		container.appendChild(buttonObj);
	},
	//组件选择框的按钮:列表和树型选择
	createButton:function(){
		//this.buttonObj = this.createQQButton(this.buttonName);
		this.buttonObj = this.createNormalButton(this.buttonName);
		var thisObj = this;
		this.buttonObj.onclick = function(){
			thisObj.toTreeSelect();
		}
		this.container.appendChild(this.buttonObj);
	},
	setCss:function(className){
		this.buttonObj.className = className;
	},
	toTreeSelect:function(thisObj){
		var url = "";

		//url = this.context + "/component/tree/view/tree.jsp?";
		url = this.context + "/component.do?component=tree&action=toTreeSelect&";
		url += "condition="+this.condition+this.paras;
		if(this.idObj!=null){
			url += "&selectIds="+this.idObj.value;
		}
		this.openWindow(url,this.windowWidth,this.windowHeight,this.id,'yes');
	},
	/**
 	* name:窗口的名称，如果打开多个相同名字的名称，后一个窗口会覆盖掉前面的窗口
 	* scrollbars:默认为没有滚动条(yes为有滚动条)
 	*/
	openWindow:function(url,width,height,name,scrollbars){
		var _height = (height!=null) ? height : "400";
		var	_width = (width != null) ? width : "600";
		var topHeight=(window.screen.height-_height)/2;
		var leftWidth=(window.screen.width-_width)/2;

		var _scrollbars = (scrollbars!=null) ? scrollbars : "no";
		var _name = (name!=null) ? name : "select";
		var win = window.open(url,_name,'height='+_height+',width='+_width+',top='+topHeight+',left='+leftWidth+',alwaysRaised=yes,depended=yes,titlebar=yes,toolbar=no,menubar=no,scrollbars='+_scrollbars+', resizable=yes,location=no, status=no,z-look=yes');		
		try{
			window.top.sub_windows.push(win);
		} catch(e){
			try{
				window.opener.top.sub_windows.push(win);
			} catch(e){  }
		}
		return win;
	},
	//打开模态窗口
	openModalWindow:function(url,width,height){
		window[this.id] = this;
		var args=[window,this];	
		
		var dialogWidth = width;
		var dialogHeight = parseInt(height);
		var isIe7 = /msie 7\.0/i.test(navigator.userAgent);//是否IE7
		if(isIe7){
			dialogHeight = dialogHeight - 40;
		}
		var sFeatures = "dialogHeight:"+dialogHeight+"px;dialogWidth:"+dialogWidth+"px;center: Yes; help: no; resizable: no; status: no;scroll:no";
		var returnValue = showModalDialog(url, args ,sFeatures);//需要将当前窗口作为参数传过去

	},
	
	////http://blog.csdn.net/juwuyi 下载
	/****************按钮创建方法开始**************************/
	createNormalButton:function(name,disabled){
		var btn = document.createElement("input");
		btn.type = "button";
		btn.className = "btn_normal";
		btn.value = (name==null)?"Normal Button":name;
		btn.disabled = (disabled==null)?"":disabled;
		btn.onclick = function(){alert();};
		if(this.container!=null) this.container.appendChild(btn);
		return btn;
	},
	createCsdnButton:function(name,disabled){
		var btn = document.createElement("button");
		btn.className = "btn_csdn";
		btn.innerHTML = (name==null)?"CSDN Button":name;
		btn.disabled = (disabled==null)?"":disabled;
		btn.onclick = function(){alert();};
		if(this.container!=null) this.container.appendChild(btn);
		return btn;
	},
	createE2003Button:function(name,disabled){
		var btn = document.createElement("button");
		btn.className = "btn_e2003";
		btn.innerHTML = (name==null)?"e商2003 Button":name;
		btn.disabled = (disabled==null)?"":disabled;;
		if(this.container!=null) this.container.appendChild(btn);
		return btn;
	},
	create2K3Button:function(name,disabled){
		var btn = document.createElement("button");
		btn.className = "btn_2k3";
		btn.innerHTML = (name==null)?"2k3 Button":name;
		btn.disabled = (disabled==null)?"":disabled;;
		if(this.container!=null) this.container.appendChild(btn);
		return btn;
	},
	createGreenButton:function(name,disabled){
		var btn = document.createElement("button");
		btn.className = "btn_green_mouseout";
		btn.onmouseover = function(){
			this.className='btn_green_mouseover';
		}
		btn.onmouseout = function(){
			this.className='btn_green_mouseout';
		}
		btn.innerHTML = (name==null)?"Green Button":name;
		btn.disabled = (disabled==null)?"":disabled;;
		if(this.container!=null) this.container.appendChild(btn);
		return btn;
	},
	createQQButton:function(name,disabled){
		var btn = document.createElement("button");
		btn.className = "btn_qq_mouseout";
		btn.onmouseover = function(){
			this.className='btn_qq_mouseover';
		};
		btn.onmouseout = function(){
			this.className='btn_qq_mouseout';
		};
		btn.onmousedown = function(){
			this.className='btn_qq_mousedown';
		};
		btn.onmouseup = function(){
			this.className='btn_qq_mouseup';
		};
		btn.innerHTML = (name==null)?"QQ Button":name;
		btn.disabled = (disabled==null)?"":disabled;;
		if(this.container!=null) this.container.appendChild(btn);
		return btn;
	}
	/****************按钮创建方法结束**************************/
}

